package com.abc.qq.view.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer1 {
    public static void main(String[] args) {
        try {
            //1.构造服务端
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("构建了ServerSocket对象");
            //2.开始监听
            System.out.println("开始监听");
            //accept会阻塞
            Socket socket = serverSocket.accept();
            //返回一个输入流
            InputStream is = socket.getInputStream();
            //返回一个输出流
            OutputStream os =  socket.getOutputStream();

            byte[] cache = new byte[100];
            is.read(cache);
            System.out.println(new String(cache).trim());

            os.write("收到啦！".getBytes());
            os.flush();

            socket.close();
            serverSocket.close();

        } catch (Exception e) {
           e.printStackTrace();
        }
    }
}
